package ch02;

import org.junit.Test;

import java.util.Random;
import java.util.Scanner;

public class HomeWorkTest {
    public static void main(String[] args) {
        double total_distance=0;
        double total_oil=0;
        for ( ;  ; ) {
            Scanner scan=new Scanner(System.in);
            System.out.println("请输入行驶里程：");
            double distance=scan.nextDouble();
            System.out.println("请输入加油量：");
            double oil=scan.nextDouble();

            if (distance==0||oil==0){
                System.out.println("输入错误，程序退出");
                break;//跳出循环
            }

            total_distance+=distance;
            total_oil+=oil;

            double oilConsumption=distance/oil;
            double avg=total_distance/total_oil;

            System.out.println("每升油行驶："+oilConsumption+"公里");
            System.out.println("平均油耗："+avg+"公里/升");

        }



    }

    @Test
    public void testCode(){
        String s="";//StringBuilder
        Random r=new Random();
        for (int i = 0; i < 6; i++) {
            if(r.nextInt(2)==0){
                int num=r.nextInt(10);
                s+=num;
            }else{
                int num=r.nextInt(65,91);
                s+=(char)num;
            }
        }
        System.out.println(s);
    }

    @Test
    public void testFor(){
        for (int i = 1; i <10; i++) {
            System.out.println(i);
        }
    }

    @Test
    public void testWhile(){
        int i=10;
        while(i<10){
            System.out.println(i);
            i++;
        }
    }

    @Test
    public void testDoWhile(){
        int i=10;
        do{
            System.out.println(i);
            i++;
        }while(i<10);
    }

    @Test
    public void testBreak(){
        for (int i = 1; i <10; i++) {
            if(i==5){
                //break;//跳出循环
                continue;//跳过本次循环
            }
            System.out.println(i);
        }
    }

    @Test
    public void test40(){
        //	设s=1*2*3*4*5*……*n，求s不大于400000时最大的n
        int s=1,i=1;
        while(s<400000)
            s=s*i++;
        System.out.println(i-2);
    }

    @Test
    public void testNumber(){
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入一个整数：");
        int num=scan.nextInt();
        //12345678
        int y;
        String s="";//缓存
        while (num!=0) {
            y=num%2;
            s=y+" "+s;
            num=num/2;
        }

//        num：0
        System.out.println(s);

    }

    @Test
    public void testFor2(){
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j < 10; j++) {
                System.out.print("*");
            }
            System.out.print("\n");
        }
    }
    @Test
    public void testFor3(){
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j < 10; j++) {
                System.out.print(i+","+j+"  ");
            }
            System.out.print("\n");
        }
    }
    @Test
    public void testFor4(){
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i+","+j+"  ");
            }
            System.out.print("\n");
        }
    }

    @Test
    public void testFor5(){
        Scanner scan=new Scanner(System.in);
        while(true) {
            System.out.println("请输入行数：");
            int row = scan.nextInt();

            for (int i = 1; i < 10; i++) {
                for (int j = 1; j < 10; j++) {
                    if (i + j == 6 || i - j == 4 || j - i == 4 || i + j == 14) {
                        System.out.print("*");
                    } else {
                        System.out.print(" ");
                    }

                }
                System.out.print("\n");
            }
        }
    }





}
